package com.sofwin.configer;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @packageName: com.sofwin.configer
 * @user: andyliu
 * @date: 2023/5/10 16:09
 * @email 115176513@qq.com
 * @description: TODO
 */
@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan(basePackages = "com.sofwin.mapper")
public class MybatisConfiger {
    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String userName;

    @Value("${jdbc.password}")
    private String password;
    @Bean
    public DruidDataSource getDruidDataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        Resource resource = new ClassPathResource("configer.xml");
        bean.setConfigLocation(resource);
        bean.setTypeAliasesPackage("com.sofwin.pojo");
        bean.setDataSource(getDruidDataSource());
        return bean.getObject();
    }

}
